package jinyilw.swing.rlaf.ui;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.basic.BasicLabelUI;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * @author Jinyi
 */
public class RLabelUI extends BasicLabelUI
{
	protected static RLabelUI rLabelUI;
	private final RenderingConfig renderingConfig = new RenderingConfig();
	public static ComponentUI createUI(JComponent c)
	{
		if (rLabelUI == null)
			rLabelUI = new RLabelUI();
		return rLabelUI;
	}

	@Override
	public void installUI(JComponent c)
	{
		super.installUI(c);
		c.setFont(FontTools.defaultFont);
	}

	@Override
	public void update(Graphics g, JComponent c)
	{

		if (c.getClientProperty(BasicHTML.propertyKey) != null)
			RThemeColor.customForegroundSetting(c);
		Graphics2D g2d = (Graphics2D) g;
		Composite composite = ComponentTools.applyEnableAlpha(g2d, c, 1f, 0.3f);
		renderingConfig.setTextAADrawing(g2d);
		super.update(g, c);
		renderingConfig.resetTextAADrawing(g2d);
		if (composite != null)
			g2d.setComposite(composite);
	}

	@Override
	protected void paintEnabledText(JLabel l, Graphics g, String s, int textX,
			int textY)
	{
		int mnemIndex = l.getDisplayedMnemonicIndex();
		Color fColor = l.getForeground();
		if (fColor == null || fColor instanceof UIResource)
			g.setColor(RThemeColor.colorSet.textColor);
		else
			g.setColor(fColor);
		BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
				textY);
	}

	@Override
	protected void paintDisabledText(JLabel l, Graphics g, String s, int textX,
			int textY)
	{
		paintEnabledText(l, g, s, textX, textY);
	}
}
